<template>
  <!-- 广告列表 -->
  <div class="app-container">
    <!-- 查询 -->
    <div class="formDiv">
      <el-form ref="form" :inline="true" :model="form" label-width="130px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="广告名称">
              <el-input v-model="form.keyword" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="上架状态">
              <el-select v-model="form.status" placeholder="请选择" clearable>
                <el-option
                  v-for="(item,index) in statusType"
                  :key="index"
                  :label="item.name"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item>
              <el-button type="primary" @click="handleSearch">查询</el-button>
              <el-button type="primary" @click="toRoutes('advertisingAdd')">添加</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <!-- 列表 -->
    <div class="listDiv" v-loading="tableLoading">
      <el-table ref="tableData" :data="tableData" style="width: 100%">
        <el-table-column
          fixed
          type="index"
          label="序号"
          width="50"
          align="center"
          :index="table_index"
        />
        <el-table-column label="广告名称" prop="name" align="center" />
        <el-table-column label="描述" prop="descript" align="center" />
        <el-table-column label="图片" prop="image" align="center">
          <template slot-scope="scope">
            <div>
              <img :src="scope.row.image | imgUrl" style="height:60px;width:60px" alt />
            </div>
          </template>
        </el-table-column>
        <el-table-column label="Ios显示平台" prop="ios" align="center" width="100px">
          <template slot-scope="scope">
            <span>{{scope.row.ios | iosFn}}</span>
          </template>
        </el-table-column>
        <el-table-column label="Android显示平台" prop="android" align="center" width="130px">
          <template slot-scope="scope">
            <span>{{scope.row.android | androidFn}}</span>
          </template>
        </el-table-column>
        <el-table-column label="有效期" align="center" width="170px">
          <template slot-scope="scope">
            <span>{{scope.row.start_time}}-{{scope.row.end_time}}</span>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" prop="created_at" align="center" width="170px">
          <template slot-scope="scope">
            <span>{{scope.row.created_at}}</span>
          </template>
        </el-table-column>
        <el-table-column label="跳转类型" prop="jump_str" align="center" width="180px" />
        <!-- <el-table-column label="链接" prop="links" align="center" /> -->
        <el-table-column label="上架状态" prop="status_name" align="center">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status"
              :active-value="1"
              :inactive-value="2"
              @change="switchChange(scope.row.id,scope.row.status)"
            />
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" align="center">
          <template slot-scope="scope">
            <el-button
              size="small"
              type="text"
              @click="handleDelete(scope.row.id)"
              v-if="scope.row.status !=1"
            >删除</el-button>
            <el-button size="small" type="text" @click="toRoutes('advertisingEdit',scope.row.id)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="pagesCon">
        <div>
          <el-pagination
            :current-page="form.page"
            :page-sizes="page_sizes"
            :page-size="form.size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page_totals"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { getList, isOn, deleteList } from "@/api/advertisingMange";
export default {
  name: "advertisingList",
  data() {
    return {
      form: {
        keyword: "",
        status: "",
        page: 1,
        size: 10
      },
      statusType: [
        { name: "全部", value: 0 },
        { name: "上架", value: 1 },
        { name: "下架", value: 2 }
      ], //上架状态
      tableData: [],
      tableLoading: true,
      page_sizes: [1, 3, 5, 10],
      page_totals: 0
    };
  },
  filters: {
    iosFn(opt) {
      switch (opt) {
        case 0:
          return "否";
          break;
        case 1:
          return "是";
          break;
      }
    },
    androidFn(opt) {
      switch (opt) {
        case 0:
          return "否";
          break;
        case 1:
          return "是";
          break;
      }
    },
    // 上下架状态
    // isonType(opt) {
    //   switch (opt) {
    //     case 1:
    //       return "上架";
    //       break;
    //     case 2:
    //       return "下架";
    //       break;
    //   }
    // },
    // 图片域名
    imgUrl(opt) {
      return localStorage.getItem("imgUrlPrefix") + opt;
    }
  },
  created() {
    this.getList(this.form);
  },
  methods: {
    // 列表
    getList(opt) {
      getList(opt)
        .then(res => {
          if (res.data.code == 200) {
            this.tableData = res.data.data;
            this.page_totals = res.data.meta.total;
          } else {
            this.$message({
              showClose: true,
              message: res.data.msg,
              type: "warning"
            });
          }
          this.tableLoading = false;
        })
        .catch(err => {
          this.tableLoading = false;
          this.errorStatus(err.msg);
          console.log("err", err);
        });
    },
    //   路由跳转
    toRoutes(name, id) {
      let route = { name, query: { id } };
      this.$router.push(route);
    },
    // 序列码
    table_index(index) {
      return (this.form.page - 1) * this.form.size + index + 1;
    },
    // 切换状态
    switchChange(id, opt) {
      let data = { id, opt };
      isOn(data).then(res => {
        if (res.data.code == 200) {
        } else {
          this.$message({
            showClose: true,
            message: res.data.msg,
            type: "warning"
          });
        }
      });
    },
    // 广告删除
    handleDelete(id) {
      let data = { id };
      this.$confirm("此操作将会删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          deleteList(data)
            .then(res => {
              if (res.data.code == 200) {
                this.succStatus("删除成功");
                this.getList();
              } else {
                this.$message({
                  showClose: true,
                  message: res.data.msg,
                  type: "warning"
                });
              }
            })
            .catch(err => {
              this.errorStatus(err.msg);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消"
          });
        });
    },
    // 广告编辑
    handleEdit() {},
    //   查询
    handleSearch() {
      this.form.page = 1;
      this.getList(this.form);
    },
    // 每页显示条数
    handleSizeChange(val) {
      this.form.size = val;
      this.getList(this.form);
    },
    // 当前页
    handleCurrentChange(val) {
      this.form.page = val;
      this.getList(this.form);
    },
    // // 弹窗组件
    eleMsg(txt, status) {
      this.$message({
        showClose: true,
        message: txt,
        type: status
      });
    },
    // 失败状态弹窗
    errorStatus(txt) {
      this.eleMsg(txt, "error");
    },
    // 成功状态弹窗
    succStatus(txt, status) {
      this.eleMsg(txt, "success");
    }
  }
};
</script>
<style scoped>
.pagesCon {
  margin-top: 20px;
  text-align: right;
}
</style>
